import set from 'lodash/set';
const regOperation=/\.\$(set|merge|apply|push|unshift|splice)$/;
/*
* 生成immutability-helper使用的更新器
* 如：genUpdater({
*   'a.b.c':1
* })=>
* {
*   a:{b:{c:{$set:1}}}
* }
* */
export const genUpdater=function(updater){
    const rst={};
    for(let key in updater){
        let path=regOperation.test(key)?key:`${key}.$set`;
        set(rst,path,updater[key]);
    }
    return rst;
};
export const getUpdaterKeys=function(updater){
    const rst=[];
    for(let key in updater){
        rst.push(key.replace(regOperation,''));
    }
    return rst;
};
export const getFixLack=(path='')=>{
    if(!path){
        return {};
    }
    const [first,...tail]=path.split('.');
    const lack=set({},tail,{});
    return {
        [first]:{
            $set:lack
        }
    }
};
export default genUpdater;